import type { Moment } from 'moment';
import moment from 'moment';
import type { Task } from '../../Task/Task';
import { DateField } from './DateField';

/**
 * Support the 'due' search instruction.
 */
export class FrontmatterOrFilenameDate extends DateField {
    public fieldName(): string {
        return 'frontmatterOrFilenameDate';
    }
    public date(task: Task): Moment | null {
        if (task.file.frontmatter && task.file.frontmatter.date) {
            const frontmatterDateMoment = moment(task.file.frontmatter.date);
            if (frontmatterDateMoment.isValid()) {
                return frontmatterDateMoment;
            } else {
                return moment('2000/01/01');
            }
        } else {
            const filenameMoment = moment(task.filename);
            if (filenameMoment.isValid()) {
                return filenameMoment;
            } else {
                return moment('2000/01/01');
            }
        }
    }
    protected filterResultIfFieldMissing() {
        return false;
    }
}
